#include "LTree.h"


LTree::LTree(double scaling_factor, double tx, double ty, double tz): scaling(scaling_factor)
{
	Fstr = "FFF-[-FX+FX+FX]+[+FX-FX-FX]";
	Xstr = "XY-[FL+FL+FL]+[+FL-FL-FL]";
	Ystr = "X+F-[FF+XY+]+L[+FF-]";
	atom = "F";
	xmin = 0.0; xmax = 0.0; ymin = 0.0; ymax = 0.0;
	shift.x = tx;
	shift.y = ty;
	shift.z = tz;
	canvas.thickness = 2.0;
	list = createTreeList();
}


LTree::~LTree(void)
{
}

void LTree::drawLeaf() const
{
	const float r = 10 + (rand()%70)*canvas.getCP().y/ymax, step = M_PI/4.0f;
	float cx = canvas.getCP().x, cy = canvas.getCP().y;
	glColor3f(0,0.5,0);
	//glPointSize(5);
	glBegin(GL_TRIANGLE_FAN);
	for (float angle=0; angle<2*M_PI; angle+=step)
		glVertex2f(cx+r*cos(angle),cy+r*sin(angle));
	glEnd();
	/*
	glColor3f(0,0,0);
	glBegin(GL_LINE_STRIP);
	for (float angle=0; angle<2*M_PI; angle+=step)
		glVertex2f(cx+r*cos(angle),cy+r*sin(angle));
	glEnd();
	*/
	glColor3f(0.5,0.25,0);

}

void LTree::draw() const
{
	glCallList(list);
}

int LTree::createTreeList(void)
{
	//setup initial turtle position
	//run through once to determine window coordinates
	canvas.setCP(0,0);
	canvas.moveTo(0.0,0.0);
	canvas.turnTo(90);
	produceString(atom, 4, 0);

	int index = glGenLists(1);
	glNewList(index,GL_COMPILE);
	
	glPushMatrix();
	/*
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();

	glPushMatrix();
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	*/
	glColor3f(0.5,0.25,0);
	glLineWidth(1);
	//glOrtho(xmin, xmax, ymin, ymax, xmin, xmax);
	//this time draw the curve
	glScaled(scaling,scaling,scaling);
	//glRotated(30,0,1,0);
	glTranslated(shift.x/scaling,shift.y/scaling,shift.z/scaling);
	canvas.moveTo(0.0,0.0);
	canvas.turnTo(90);
	canvas.thickness = 4.0;
	produceString(atom, 4, 1);
	
	glPopMatrix();
	/*glPopMatrix();
	*/
	glEndList();
	return index;
}
